
package com.tools.cleanmaster.manager;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.utils.PackageUtils;

public class RateManager {

    public static final String GO_RATE = "go_rate";

    public enum RateType {
        Optimize, ChangeSkin, MainTabNewUser
    }

    public enum DetailRateType {
        OptimizeNew, OptimizeAgainNew, OptimizeMore, ChangeSkinNew, MainTabNewUserRate
    }

    private static final String TAG = "RateManager";
    private static final String RATE_RECORD_NEW = "rate_record_new";
    private static final String HAD_RATED_NEW = "had_rated_new";

    //用于记录新用户第3次进入Tab的rate策略
    private static final String TAB_RATE_ENTER_NUM = "tabRateEnterNum";
    private static final String TAB_RATE_FOR_NEW_ENABLE = "isTabRateForNewEnable";

    private static final int OPTIMIZE_MIN_REMAIN_TIME = 60 * 10;
    private static final int OPTIMIZE_SECOND_TIME = 60 * 15;
    private static final int OPTIMIZE_MORE_TIME = 60 * 30;

    private static final long DEFAULT_CURTIME = -1;
    private static final long SCENE_TRIGGER_INTERVAL = CleanConstant.TIME_ONE_DAY / 2;

    private static RateManager mRateManager;

    private Context mContext;
    private SharedPreferences mSP;
    private DetailRateType mCurDRT;

    private boolean mHadRated = false;
    private long mFirstOptimizeTime, mSecondOptimizeTime, mMoreOptimizeTime, mChangedSkinTime;

    public static RateManager getInstance(Context ctx) {
        if (mRateManager == null) {
            mRateManager = new RateManager(ctx);
        }
        return mRateManager;
    }

    private RateManager(Context ctx) {

        mContext = ctx.getApplicationContext();
        mSP = mContext.getSharedPreferences(RATE_RECORD_NEW, Context.MODE_PRIVATE);
        mHadRated = mSP.getBoolean(HAD_RATED_NEW, false);

        mFirstOptimizeTime = mSP.getLong(DetailRateType.OptimizeNew.name(), DEFAULT_CURTIME);
        mSecondOptimizeTime = mSP.getLong(DetailRateType.OptimizeAgainNew.name(), DEFAULT_CURTIME);
        mMoreOptimizeTime = mSP.getLong(DetailRateType.OptimizeMore.name(), DEFAULT_CURTIME);

        mChangedSkinTime = mSP.getLong(DetailRateType.ChangeSkinNew.name(), DEFAULT_CURTIME);
    }

    public static Intent getRateIntent(Context context) {
        Uri uri = Uri.parse("http://market.android.com/details?id=" + context.getPackageName());
        Intent it = new Intent(Intent.ACTION_VIEW, uri);
        if (PackageUtils.isAppInstalled(context, "com.android.vending")) {
            it.setPackage("com.android.vending");
        } else {
            Intent defaultBrowser = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://www.google.com"));
            ResolveInfo ri = context.getPackageManager().resolveActivity(defaultBrowser,
                    PackageManager.MATCH_DEFAULT_ONLY);
            if (ri == null) {
                return null;
            }
        }
        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return it;
    }

    //用于主Tab页面Rate提示
    public boolean isTabRateEnable() {
        return mSP.getBoolean(TAB_RATE_FOR_NEW_ENABLE, true);
    }

    public void setTabRateEnable(boolean value) {
        mSP.edit().putBoolean(TAB_RATE_FOR_NEW_ENABLE, value).apply();
    }

    private int getTabRateEnterNumber(){
        return mSP.getInt(TAB_RATE_ENTER_NUM, 1);
    }
    private void setTabRateEnterNumber(int value){
        mSP.edit().putInt(TAB_RATE_ENTER_NUM, value).apply();
    }
}
